// pages/cutFace/index.js

const WeCropper = require('../../we-cropper/we-cropper.js');
const app = getApp();
const device = wx.getSystemInfoSync(); // 获取设备信息
const width = device.windowWidth; // 示例为一个与屏幕等宽的正方形裁剪框
const system = device.system;
var w = app.getRatio();
let height;
if (system.indexOf('iOS') != -1) {
  height = device.windowHeight - 100;
} else {
  height = device.windowHeight - app.data.navHeight - 130 / 2 * w;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: app.data.navHeight,
    rotateI: 0,
    cropperOpt: {
      id: 'cropper',
      rotateI: 0,
      tranlateX: width / 2,
      tranlateY: height / 2,
      width,  // 画布宽度
      height, // 画布高度
      scale: 2.5, // 最大缩放倍数
      zoom: 8, // 缩放系数,
      cut: { // 裁剪框的坐标 大小
        x: -width / 2, 
        y: -(height - width) / 2,
        width: width,
        height: width
      }
    },
    chooseImg: false,
    imgSrc: '',
    marTop: 40
  },
  onLoad: function (options) {
    const self = this;
    const system = device.system;
    var cutFace = app.data.jsonPage.cutFace;
    var nvabarData = {
      title: cutFace.title,
      showCapsule: true
    }
    if (system.indexOf('iOS') != -1) {
      this.setData({
        ios: true
      })
    };
    if (device.model.indexOf("iPhone X") != -1) {
      this.setData({
        height: wx.getStorageSync('height') * 2 + 50,
      })
    };
    var cut ;
    if (options.touxiang == "true"){
       cut = { // 裁剪框的坐标 大小
            x: -width / 2,
            y: -(height - width) / 2,
            width: width,
            height: width
          }
    }else{
       cut = { // 裁剪框的坐标 大小
         x: -width / 2,
         y: -(height - (width / 1.4)) / 2,
         width: width,
         height: width / 1.4
      }
    }
    this.setData({
      cut: cut,
      nvabarData: nvabarData,
      cuttype: options.cuttype
    })
    const { cropperOpt } = this.data;
    cropperOpt.cut = cut;
    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        self.wecropper.updateCanvas(this.data.rotateI)
      })
      .on('beforeImageLoad', (ctx) => {
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        wx.hideToast()
      })
    this.chooseImg()

  },

  chooseImg() {
    const self = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        if (src) {
          // 将图片参数传递给插件
          self.wecropper.pushOrign(src);
          self.data.chooseImg=true;
          self.data.imgSrc=src;
          self.data.rotateI=0;
        };
        wx.hideToast()
      },
      fail(res) {
        wx.hideToast();
        wx.navigateBack()
      }
    })
  },

  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
 /* 确定保存后返回上级 */ 
  getCropperImage() {
    let that = this;
    if (this.data.chooseImg) {
      this.wecropper.getCropperImage((src) => {
        if (src) {
          if (this.data.cuttype == 1) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2]
            prevPage.setData({
              IDCard: src,
              cuttype: this.data.cuttype
            })
          } else if (this.data.cuttype == 2) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2]
            prevPage.setData({
              hornerCard: src,
              cuttype: this.data.cuttype
            })
          } else if (this.data.cuttype == 3) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2];
            prevPage.setData({
              licenceCard: src,
              cuttype: this.data.cuttype
            })
          } else if (this.data.cuttype == 4) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 2]
            prevPage.setData({
              certificationCard: src,
              cuttype: this.data.cuttype
            })
          };
          wx.navigateBack()
        } else {
          wx.hideToast()
          wx.showToast({
            title: '获取图片地址失败，请稍后再试！',
          })
        }
      })
    } else {
      wx.showToast({
        title: '您还没选择图片！',
        icon: 'none'
      })
    }

  },

  cancleCropper() {
    wx.hideToast()
    wx.navigateBack()
  },

  // 图片旋转
  rotateImg() {
    const self = this;
    let rotateI = this.data.rotateI + 1;
    this.setData({
      rotateI: rotateI
    })
    self.wecropper.updateCanvas(rotateI)
  }
})